<template>
  <div class="app-container">
    <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="demo-form-inline"
      v-hasPermi="['emp:roster:query']">
      <el-form-item label="工号" prop="employeeNo">
        <el-input v-model.trim="queryParams.employeeNo" size="small" clearable :maxlength="10" placeholder="请输入工号"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model.trim="queryParams.name" clearable size="small" :maxlength="10" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="department">
        <el-input v-model.trim="queryParams.department" size="small" clearable :maxlength="100" placeholder="请输入部门"></el-input>
      </el-form-item>
      <el-form-item label="职务" prop="position">
        <el-input v-model.trim="queryParams.position" size="small" clearable :maxlength="20" placeholder="请输入职务"></el-input>
      </el-form-item>
      <el-form-item label="员工类型" prop="employeeType">
        <el-select v-model="queryParams.employeeType" size="small" clearable placeholder="请选择员工类型">
          <el-option v-for="dict in dict.type.sys_employee_type" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工状态" prop="employeeStatus">
        <el-select v-model="queryParams.employeeStatus" size="small" clearable placeholder="请选择员工状态">
          <el-option v-for="dict in dict.type.sys_employee_status" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" v-hasPermi="['emp:roster:query']"
          @click="searchHandler">查询</el-button>
        <el-button icon="el-icon-refresh-right" size="mini" v-hasPermi="['emp:roster:query']"
          @click="resetForm('queryFormRef')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="20">
      <el-col :span="24">
        <div class="grid-content bg-purple">
          <el-button plain type="primary" size="mini" icon="el-icon-plus" v-hasPermi="['emp:roster:add']"
            @click="openDialog(null)">增加</el-button>
          <el-button plain type="info" size="mini" icon="el-icon-upload" v-hasPermi="['emp:roster:import']"
            @click="handleImport">导入</el-button>
          <el-button plain type="warning" size="mini" icon="el-icon-download" v-hasPermi="['emp:roster:export']"
            @click="exportHandler">导出</el-button>
        </div>
      </el-col>
    </el-row>

    <el-table
      ref="tableRef"
      :data="tableData"
      highlight-current-row
      max-height="620"
      v-loading="loading"
      v-horizontal-scroll="'auto'"
      style="width: 100%;margin-top: 10px;"
    >
      <el-table-column type="index" width="50" align="center" label="序号">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column property="name" label="姓名" align="center" />
      <el-table-column property="employeeNo" label="工号" align="center" />
      <el-table-column property="gender" label="性别" align="center">
        <template slot-scope="scope">
          {{ getListValue(scope.row.gender, dict.type.sys_user_sex) }}
        </template>
      </el-table-column>
      <el-table-column property="ethnicity" label="民族" align="center" />
      <el-table-column property="birthDate" label="出生日期" width="100" align="center" />
      <el-table-column property="age" label="年龄" align="center" />
      <el-table-column property="idCard" label="身份证号码" align="center" width="170" />
      <el-table-column property="politicalStatus" label="政治面貌" align="center" />
      <el-table-column property="householdType" label="户口类型" align="center" />
      <el-table-column property="phone" label="手机" width="110" align="center" />
      <el-table-column property="department" label="部门" align="center" width="240" />
      <el-table-column property="position" label="职务" align="center" />
      <el-table-column property="jobTitle" label="职位" align="center" />
      <el-table-column property="positionLevel" label="职级" align="center" />
      <el-table-column property="hireDate" label="入职日期" width="100" align="center" />
      <el-table-column property="companyYears" label="司龄(年)" align="center" />
      <el-table-column property="employeeType" label="员工类型" align="center" />
      <!-- <el-table-column property="hireDate" label="入职日期" width="100" align="center" /> -->
      <el-table-column property="employeeStatus" label="员工状态" align="center" />
      <el-table-column property="currentAddress" label="现居住地址" align="center" width="120" />
      <el-table-column property="education" label="学历" align="center" />
      <el-table-column property="graduationSchool" label="毕业院校" align="center" width="120" />
      <el-table-column property="graduationDate" label="毕业时间" width="100" align="center" />
      <el-table-column property="major" label="专业" align="center" width="120" />
      <el-table-column property="degree" label="学位" align="center" />
      <el-table-column property="emergencyContact" label="紧急联系人" align="center" width="90" />
      <el-table-column property="emergencyPhone" label="紧急联系人手机" align="center" width="120" />
      <el-table-column property="remark" label="备注" align="center" />
      <el-table-column label="操作" align="center" v-if="$auth.hasPermi('emp:roster:edit') || $auth.hasPermi('emp:roster:remove')">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" v-hasPermi="['emp:roster:edit']"
            @click="openDialog(scope.row)">修改</el-button>
          <el-popconfirm confirm-button-text="是" cancel-button-text="否" title="确定删除吗？"
            @confirm="deleteHandler(scope.row)">
            <el-button slot="reference" icon="el-icon-delete" v-hasPermi="['emp:roster:remove']"
              type="text" style="margin-left: 10px">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :title="dialogTitle === 1 ? '新增' : '修改'" :visible.sync="dialogVisible" close-on-click-modal
      width="900px" :fullscreen="device === 'mobile'">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="110px" class="demo-form" v-if="device ==='mobile'" label-position="top">
        <el-row :gutter="10">
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="姓名" prop="name">
              <el-input v-model.trim="form.name" clearable :maxlength="10" placeholder="请输入姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="工号" prop="employeeNo">
              <el-input v-model.trim="form.employeeNo" clearable :maxlength="10" placeholder="请输入工号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="form.gender" clearable placeholder="请选择性别" style="width: 100%">
                <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="民族" prop="ethnicity">
              <el-input v-model.trim="form.ethnicity" clearable :maxlength="20" placeholder="请输入民族" style="width: 100%;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="出生日期" prop="birthDate">
              <el-date-picker v-model="form.birthDate" type="date" clearable value-format="yyyy-MM-dd"
                placeholder="选择出生日期" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="身份证号码" prop="idCard">
              <el-input v-model.trim="form.idCard" clearable :maxlength="20" placeholder="请输入身份证号码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="政治面貌" prop="politicalStatus">
              <el-select v-model="form.politicalStatus" clearable placeholder="请选择政治面貌" style="width: 100%">
                <el-option v-for="dict in dict.type.sys_political_status" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="户口类型" prop="householdType">
              <el-select v-model="form.householdType" clearable placeholder="请选择户口类型" style="width: 100%">
                <el-option v-for="dict in dict.type.sys_household_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="手机" prop="phone">
              <el-input v-model.trim="form.phone" clearable :maxlength="20" placeholder="请输入手机号码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="部门" prop="department">
              <el-input v-model.trim="form.department" clearable :maxlength="500" placeholder="请输入部门"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="职务" prop="position">
              <el-input v-model.trim="form.position" clearable :maxlength="20" placeholder="请输入职务"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="职位" prop="jobTitle">
              <el-input v-model.trim="form.jobTitle" clearable :maxlength="20" placeholder="请输入职位"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="职级" prop="positionLevel">
              <el-input v-model.trim="form.positionLevel" clearable :maxlength="20" placeholder="请输入职级"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="入职日期" prop="hireDate">
              <el-date-picker v-model="form.hireDate" type="date" clearable value-format="yyyy-MM-dd"
                placeholder="选择入职日期" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="员工类型" prop="employeeType">
              <el-select v-model="form.employeeType" clearable placeholder="请选择员工类型" style="width: 100%">
                <el-option v-for="dict in dict.type.sys_employee_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="员工状态" prop="employeeStatus">
              <el-select v-model="form.employeeStatus" clearable placeholder="请选择员工状态" style="width: 100%">
                <el-option v-for="dict in dict.type.sys_employee_status" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 24 : 16">
            <el-form-item label="现居住地址" prop="currentAddress">
              <el-input v-model.trim="form.currentAddress" clearable :maxlength="100" placeholder="请输入现居住地址"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="学历" prop="education">
              <el-input v-model.trim="form.education" clearable :maxlength="20" placeholder="请输入学历"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="毕业院校" prop="graduationSchool">
              <el-input v-model.trim="form.graduationSchool" clearable :maxlength="50" placeholder="请输入毕业院校"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="毕业时间" prop="graduationDate">
              <el-date-picker v-model="form.graduationDate" type="date" clearable value-format="yyyy-MM-dd"
                placeholder="选择毕业时间" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="专业" prop="major">
              <el-input v-model.trim="form.major" clearable :maxlength="50" placeholder="请输入专业"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="学位" prop="degree">
              <el-input v-model.trim="form.degree" clearable :maxlength="50" placeholder="请输入学位"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="紧急联系人" prop="emergencyContact">
              <el-input v-model.trim="form.emergencyContact" clearable :maxlength="10"
                placeholder="请输入紧急联系人姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="device === 'mobile' ? 12 : 8">
            <el-form-item label="紧急联系人手机" prop="emergencyPhone">
              <el-input v-model.trim="form.emergencyPhone" clearable :maxlength="20"
                placeholder="请输入紧急联系人手机"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="form.remark" type="textarea" :maxlength="200" placeholder="请输入备注" :autosize="{ minRows: 2 }"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="110px" class="demo-form" v-else>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="姓名" prop="name">
              <el-input v-model.trim="form.name" clearable :maxlength="10" placeholder="请输入姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工号" prop="employeeNo">
              <el-input v-model.trim="form.employeeNo" clearable :maxlength="10" placeholder="请输入工号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="form.gender" clearable placeholder="请选择性别" style="width: 100%">
                <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="民族" prop="ethnicity">
              <el-input v-model.trim="form.ethnicity" clearable :maxlength="20" placeholder="请输入民族" style="width: 100%;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出生日期" prop="birthDate">
              <el-date-picker v-model="form.birthDate" type="date" clearable value-format="yyyy-MM-dd"
                placeholder="选择出生日期" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="身份证号码" prop="idCard">
              <el-input v-model.trim="form.idCard" clearable :maxlength="20" placeholder="请输入身份证号码"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="政治面貌" prop="politicalStatus">
              <el-select v-model="form.politicalStatus" clearable placeholder="请选择政治面貌" style="width: 100%">
                <el-option v-for="dict in dict.type.sys_political_status" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="户口类型" prop="householdType">
              <el-select v-model="form.householdType" clearable placeholder="请选择户口类型" style="width: 100%">
                <el-option v-for="dict in dict.type.sys_household_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="手机" prop="phone">
              <el-input v-model.trim="form.phone" clearable :maxlength="20" placeholder="请输入手机号码"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="部门" prop="department">
              <el-input v-model.trim="form.department" clearable :maxlength="500" placeholder="请输入部门"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="职务" prop="position">
              <el-input v-model.trim="form.position" clearable :maxlength="20" placeholder="请输入职务"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="职位" prop="jobTitle">
              <el-input v-model.trim="form.jobTitle" clearable :maxlength="20" placeholder="请输入职位"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="职级" prop="positionLevel">
              <el-input v-model.trim="form.positionLevel" clearable :maxlength="20" placeholder="请输入职级"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入职日期" prop="hireDate">
              <el-date-picker v-model="form.hireDate" type="date" clearable value-format="yyyy-MM-dd"
                placeholder="选择入职日期" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="员工类型" prop="employeeType">
              <el-select v-model="form.employeeType" clearable placeholder="请选择员工类型" style="width: 100%">
                <el-option v-for="dict in dict.type.sys_employee_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="员工状态" prop="employeeStatus">
              <el-select v-model="form.employeeStatus" clearable placeholder="请选择员工状态" style="width: 100%">
                <el-option v-for="dict in dict.type.sys_employee_status" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="16">
            <el-form-item label="现居住地址" prop="currentAddress">
              <el-input v-model.trim="form.currentAddress" clearable :maxlength="100" placeholder="请输入现居住地址"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="学历" prop="education">
              <el-input v-model.trim="form.education" clearable :maxlength="20" placeholder="请输入学历"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="毕业院校" prop="graduationSchool">
              <el-input v-model.trim="form.graduationSchool" clearable :maxlength="50" placeholder="请输入毕业院校"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="毕业时间" prop="graduationDate">
              <el-date-picker v-model="form.graduationDate" type="date" clearable value-format="yyyy-MM-dd"
                placeholder="选择毕业时间" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="专业" prop="major">
              <el-input v-model.trim="form.major" clearable :maxlength="50" placeholder="请输入专业"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学位" prop="degree">
              <el-input v-model.trim="form.degree" clearable :maxlength="50" placeholder="请输入学位"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="紧急联系人" prop="emergencyContact">
              <el-input v-model.trim="form.emergencyContact" clearable :maxlength="10"
                placeholder="请输入紧急联系人姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="紧急联系人手机" prop="emergencyPhone">
              <el-input v-model.trim="form.emergencyPhone" clearable :maxlength="20"
                placeholder="请输入紧急联系人手机"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="form.remark" type="textarea" :maxlength="200" placeholder="请输入备注" :autosize="{ minRows: 2 }"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('formRef')">提交</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="导入" :visible.sync="upload.open" width="400px" :fullscreen="device =='mobile'">
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess"
        :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { addDataApi, deleteDataApi, getDataListApi, updateDataApi } from "@/api/crm/emp/roster";
import ProvinceData from '@/views/crm/data/province.json'
import { getUserProfile } from "@/api/system/user";
import { getToken } from "@/utils/auth";
import { mapGetters } from 'vuex'

export default {
  name: 'RosterView',
  dicts: [
    'sys_user_sex',
    'sys_household_type',
    'sys_political_status',
    'sys_employee_type',
    'sys_employee_status'
  ],
  data() {
    return {
      // 记录上一次打开弹窗是修改or增加
      // null 增加
      // 非null 修改
      formId: null,
      // 加载动画
      loading: true,
      pickerOptions: {
        disabledDate(time) {
          // 禁止选择今天及今天之后的日期
          return time.getTime() > Date.now();
        }
      },
      // 活动日期
      activityDate: [],
      // 省份数据
      provinceList: [],
      // 弹框标题 1是增加 其他是修改
      dialogTitle: 1,
      // 导入弹框显示
      importVisible: false,
      // 弹框显示
      dialogVisible: false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新数据
        updateSupport: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/erp/employee/importData"
      },
      // 新增和修改参数
      form: {
        name: "", // 姓名
        employeeNo: "", // 工号
        gender: "", // 性别
        ethnicity: "", // 民族
        birthDate: "", // 出生日期
        age: null, // 年龄
        idCard: "", // 身份证号码
        householdType: "", // 户口类型
        phone: "", // 手机
        department: "", // 部门
        position: "", // 职务
        jobTitle: "", // 职位
        positionLevel: "", // 职级
        politicalStatus: "", // 政治面貌
        hireDate: "", // 入职日期
        companyYears: "", // 司龄
        employeeType: "", // 员工类型
        employeeStatus: "", // 员工状态
        currentAddress: "", // 现居住地址
        education: "", // 学历
        graduationSchool: "", // 毕业院校
        graduationDate: "", // 毕业时间
        major: "", // 专业
        degree: "", // 学位
        emergencyContact: "", // 紧急联系人姓名
        emergencyPhone: "", // 紧急联系人手机
        remark: "", // 备注
      },
      // 重点客户下拉数据
      isImportantList: [
        {
          value: 0,
          label: '普通客户'
        },
        {
          value: 1,
          label: '重点客户'
        }
      ],
      // 表单的校验规则
      rules: {
        employeeNo: [
          { required: true, message: '请填写工号', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        ethnicity: [
          { required: true, message: '请填写民族', trigger: 'blur' }
        ],
        birthDate: [
          { required: true, message: '请选择出生日期', trigger: 'change' }
        ],
        idCard: [
          { required: true, message: '请填写身份证号码', trigger: 'blur' }
        ],
        // fourthStartDate: [
        //   {required: true, message: '请选择第四季度日期范围', trigger: 'change'}
        // ],
        // year: [
        //   {required: true, message: '请选择财年', trigger: 'change'}
        // ],
        name: [
          { required: true, message: '请填写姓名', trigger: 'blur' }
        ],
        // activityDate: [
        //   {required: true, message: '请选择日期', trigger: 'change'}
        // ],
      },
      // 表格数据
      tableData: [],
      // 总数条数
      total: 0,
      // 查询参数
      queryParams: {
        employeeNo: "", // 工号
        name: "", // 姓名
        department: "", // 部门
        position: "", // 职务
        employeeType: "", // 员工类型
        employeeStatus: "", // 员工状态
        pageNum: 1,
        pageSize: 10,
      },
      // 用户信息
      user: {},
      roleGroup: {},
      postGroup: {},
    }
  },
  computed:{
    ...mapGetters([
      'device'
    ])
  },
  watch: {
    'form.isImportant': {
      handler(newV) {
        if (newV !== 1) {
          this.form.basis = null
        }
      }
    },
  },
  created() {
    this.provinceList = ProvinceData
    this.getList()
    this.getUserinfo()
  },
  activated() {
    this.getList()
  },
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    // 获取根据list中的value返回label
    getListValue(data, list, value = 'value') {
      let label = ''
      list.map((item) => {
        if (+item[value] === +data) {
          label = item.label
        }
      })
      return label
    },
    // 获取用户信息
    getUserinfo() {
      getUserProfile().then(response => {
        this.user = response.data;
        this.roleGroup = response.roleGroup;
        this.postGroup = response.postGroup;
      });
    },
    // 获取数据
    async getList() {
      this.loading = true
      const res = await getDataListApi(this.queryParams)
      if (res.code === 200) {
        this.tableData = res.rows
        this.total = res.total
      }
      this.loading = false
    },
    // 搜索的回调
    searchHandler() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 删除的回调
    async deleteHandler(row) {
      const res = await deleteDataApi({
        id: row.id
      })
      if (res.code === 200) {
        this.$message({
          message: res.msg,
          type: 'success'
        });
        this.getList()
        this.dialogVisible = false
      } else {
        this.$message({
          message: res.msg,
          type: 'error'
        });
      }
    },
    // 打开弹窗
    openDialog(row) {
      if (row) {
        this.form = { ...row }
        this.formId = row.id
        this.dialogTitle = 2
      } else {
        if(this.formId) {
          this.form = { ...this.$options.data().form }
        }
        this.formId = null
        this.dialogTitle = 1
      }
      this.dialogVisible = true
      // this.resetForm('formRef')
      this.$nextTick(()=>{
        this.$refs['formRef'].clearValidate();
        // this.resetForm('formRef')
      })
    },
    // 导出的回调
    async exportHandler() {
      this.download('erp/employee/export', {
        ...this.queryParams,
        userId: this.user.userId,
        deptId: this.user.deptId,
      }, `employees_${new Date().getTime()}.xlsx`)
    },
    // 表单提交
    submitForm(formName) {
      // if(this.form.isImportant === 1 && !(this.form.basis)) {
      //   this.$notify({
      //     title: '警告',
      //     message: '请选择重点依据',
      //     type: 'warning'
      //   });
      //   return false
      // }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.dialogTitle === 1) {
            this.addDataHandler()
          } else {
            this.updateDataHandler()
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 增加数据的回调
    async addDataHandler() {
      const payload = {
        ...this.form,
        createBy: this.user.nickName,
        updateBy: this.user.nickName,
        userId: this.user.userId,
      }
      const res = await addDataApi(payload)
      if (res.code === 200) {
        this.$message({
          message: res.msg,
          type: 'success'
        });
        this.getList()
        this.formId = -1
        this.dialogVisible = false
        this.$refs['formRef'].resetFields();
      } else {
        this.$message({
          message: res.msg,
          type: 'error'
        });
      }
    },
    // 修改数据的回调
    async updateDataHandler() {
      const payload = {
        ...this.form,
        updateBy: this.user.nickName,
      }
      const res = await updateDataApi(payload)
      if (res.code === 200) {
        this.$message({
          message: res.msg,
          type: 'success'
        });
        this.getList()
        this.dialogVisible = false
      } else {
        this.$message({
          message: res.msg,
          type: 'error'
        });
      }
    },
    // 重置表单
    resetForm(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].resetFields();
      }
    },
    // 重置的回调
    resetHandler() {
      this.activityDate = []
      this.resetForm('queryFormRef')
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 下载模板操作 */
    importTemplate() {
      const a = document.createElement('a')
      a.href = '/xlsx/employee_template.xlsx'
      a.click()
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
  },
}
</script>

<style scoped lang="scss"></style>
